package ControlLayer;
import java.util.ArrayList;

import ModelLayer.*;

/**
 * Write a description of class LoanCtr here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanCtr
{
    // instance variables - replace the example below with your own
    private ArrayList<Loan> loans;

    /**
     * Constructor for objects of class LoanCtr
     */
    public LoanCtr()
    {
        // initialise instance variables
        loans = new ArrayList<Loan>();
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public void createLoan(long _loanId, String _borrowDate, int _period, long _personId, long _dvdId, long _copyId)
    {
        // put your code here
        Loan l = new Loan();
        if(this.getLoan(_loanId)==null)
        {
            if(DVDCtr.getDvd(_dvdId)!=null)
            {
                if(AddressCtr.getPerson(_personId)!=null)
                {
                    if(DVDCtr.getDvd(_dvdId).getCopy(_copyId)!=null)
                    {
                        l.setId(_loanId);
                        l.setBorrowDate(_borrowDate);
                        l.setPeriod(_period);
                        l.addCopy(DVDCtr.getDvd(_dvdId).getCopy(_copyId));
                        l.setOwner(AddressCtr.getPerson(_personId));
                        loans.add(l);
                        System.out.println("Successfully Added!");
                    }
                    else
                     System.out.println("The copy does not exist");
                }
                else
                    System.out.println("The Person does not exist");
            }
            else
                System.out.println("The DVD does not exist");
        }
        else
            System.out.println("The loan allready exists");
    }
    
    public Loan getLoan(long _loanId)
    {
        Loan l = new Loan();
        l = null;
        for (int i=0; i<loans.size(); i++)
        {
            if (loans.get(i).getId() == _loanId)
                l = loans.get(i);
            else
                l = null;
        }
        
        return l;
    }
    
    public void updateLoan(Loan _loan)
    {
        if (getLoan(_loan.getId()) != null)
        {
            getLoan(_loan.getId()).setBorrowDate(_loan.getBorrowDate());
            getLoan(_loan.getId()).setPeriod(_loan.getPeriod());
            System.out.println("Successfully updated!");
        }
        else
            System.out.println("The loan does not exist!");
    }
    
    public void deleteLoan(long _loanId)
    {
        if (this.getLoan(_loanId) != null)
        {
            for (int i=0; i<loans.size(); i++)
            {
                loans.remove(i);
                System.out.println("Successfully deleted!");
            }
        }
        else
            System.out.println("The Loan is not present in the system!");
    }
    
    public ArrayList<Loan> listAllLoans()
    {
        return loans;
    }
    
    public void addCopy(long _loanId, Copy _copy)
    {
        if (this.getLoan(_loanId).getCopy(_copy.getSerialNumber()) == null)
        {
            this.getLoan(_loanId).addCopy(_copy);
            System.out.println("Successfully added!");
        }
        else
            System.out.println("This copy is allready present in the loan");
    }
    
    public ArrayList<DVD> listAllDvds()
    {
        return DVDCtr.listAllDvd();
    }
    
    public ArrayList<Copy> listAllCopies(long _dvdId)
    {
        return DVDCtr.getDvd(_dvdId).listAllCopies();
    }
    
    public ArrayList<Person> listAllPerson()
    {
        return AddressCtr.listAllPersons();
    }
}
